<template>
  <div class="app-container">
    <el-form
      ref="queryParams"
      :model="queryParams"
      size="small"
      label-position="right"
      label-suffix=":"
      label-width="120px"
    >
      <el-row :gutter="10">
        <el-col :span="1.5">
          <el-form-item :label="$t('unownedGoodsLang.unownedStatus')">
            <el-radio-group v-model="queryParams.orderStatus">
              <el-radio-button label="null">{{ $t("all") }}</el-radio-button>
              <el-radio-button
                v-for="item in unownedStatusOptions"
                :key="item.key"
                :label="item.value"
              >
                {{ $t("statusLang." + item.title) }}
              </el-radio-button>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="10">
        <el-col :span="1.5">
          <el-form-item>
            <template #label>
              <el-select v-model="noType">
                <el-option
                  :value="0"
                  :label="$t('backOrderLang.backOrderNum')"
                />
              </el-select>
            </template>
            <el-input
              v-model="anyNo"
              :placeholder="$t('backOrderLang.backPleaseEnterTip')"
              type="textarea"
              :rows="3"
              style="width: 350px"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="10">
        <el-col :span="1.5">
          <el-form-item>
            <template #label>
              <el-select v-model="timeType">
                <el-option :value="0" :label="$t('backOrderLang.createDate')" />
                <el-option :value="1" :label="$t('backOrderLang.backAcceptDate')" />
              </el-select>
            </template>
            <el-date-picker
              v-model="timeRange"
              type="daterange"
              value-format="timestamp"
              start-placeholder="开始日期"
              range-separator="至"
              end-placeholder="结束日期"
              style="width: 350px"
              clearable
            />
          </el-form-item>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="primary"
            size="small"
            icon="el-icon-search"
            @click="handleQuery"
          >
            {{ $t("query") }}
          </el-button>
          <el-button
            type="primary"
            size="small"
            icon="el-icon-refresh-left"
            @click="handleReset"
          >
            {{ $t("reset") }}
          </el-button>
        </el-col>
      </el-row>
    </el-form>
    <el-table
      ref="table"
      v-loading="loading"
      :data="tableData"
      border
      class="mt-10"
    >
      <el-table-column
        :label="$t('tableIndex')"
        type="index"
        :index="indexAdd"
        align="center"
        width="100"
      />
      <el-table-column
        :label="$t('backOrderLang.backInfo')"
        prop="returnNo,status"
        align="left"
        min-width="150"
      >
        <template slot-scope="scope">
          <p>
            {{ $t("backOrderLang.backOrderNum") + ":" }}
            <span>{{ scope.row.returnNo }}</span>
          </p>
          <p>
            {{ $t("unownedGoodsLang.unownedStatus") + ":" }}
            <span>{{ $t("statusLang." + getTitleByValue(unownedStatusOptions, scope.row.status)) }}</span>
          </p>
        </template>
      </el-table-column>
      <el-table-column
        :label="$t('unownedGoodsLang.goodsNum')"
        prop="locationCode"
        align="center"
        min-width="150"
      />
      <el-table-column
        :label="$t('backOrderLang.date')"
        prop="gmtCreated,createdUserName,validTime"
        align="left"
        min-width="150"
      >
        <template slot-scope="scope">
          <p>
            {{ $t("cutOffOrderLang.createDate") + ":" }}
            <span>{{ scope.row.gmtCreated }}</span>
          </p>
          <p>
            {{ $t("cutOffOrderLang.creator") + ":" }}
            <span>{{ scope.row.createdUserName }}</span>
          </p>
          <p>
            {{ $t("unownedGoodsLang.effectiveDate") + ":" }}
            <span>{{ scope.row.validTime }}</span>
          </p>
        </template>
      </el-table-column>
      <el-table-column
        :label="$t('operation')"
        width="100"
        align="center"
        fixed="right"
      >
        <template slot-scope="scope">
          <el-button type="text" @click="handleDetail(scope.row)">
            {{ $t("info") }}
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 页码 -->
    <el-row type="flex" justify="end" align="middle" style="height: 60px">
      <el-pagination
        :total="total"
        layoout="total,prev,pager,next"
        :current-page="queryParams.pageNo"
        :page-size="queryParams.pageSize"
        @current-change="pageNoChange"
      />
    </el-row>
    <!-- 新增/编辑弹层 -->
    <DepotBaseForm
      v-model="infoParams.id"
      :visible.sync="depotDialog"
      :title="title"
      :operation="infoParams.operation"
      @close="close"
    />
  </div>
</template>

<script>
import DepotBaseForm from './components/info-form.vue'
import { unownedIndexStatusOptionDict } from '@/utils/dictData'
import { getDictOptionList, getTitleByValue } from '@/utils/dictUtils'
import { exportStockList } from '@/api/business/inventoryManage/inventory'
import { getPageList } from '@/api/business/operateManage/unownedGoods'

export default {
  name: 'Depot',
  components: { DepotBaseForm },
  data() {
    return {
      noType: 0,
      anyNo: null,
      timeType: 0,
      timeRange: [],
      getTitleByValue: getTitleByValue,
      unownedStatusOptions: getDictOptionList(unownedIndexStatusOptionDict),
      total: 0,
      loading: false,
      queryParams: {
        orderStatus: null,
        pageNo: 1,
        pageSize: 10
      },
      tableData: [],
      infoParams: {
        id: null,
        operation: 0
      },
      depotDialog: false,
      title: ''
    }
  },
  created() {
    this.getDepotPageList()
  },
  methods: {
    // 重置
    async handleReset() {
      this.queryParams = {
        orderStatus: null,
        pageNo: 1,
        pageSize: 10
      }
      this.noType = 0
      this.anyNo = null
      this.timeRange = []
      await this.$refs.queryParams.resetFields()
      this.getDepotPageList()
    },
    handleOpenInfo(row) {
      this.infoParams.operation = 0
      this.infoParams.id = row.id
      this.title = "$t('info')"
      setTimeout(() => {
        this.depotDialog = true
      }, 400)
    },
    // 查询
    async handleQuery() {
      this.queryParams.pageNo = 1
      this.getDepotPageList()
    },
    // 获取列表
    async getDepotPageList() {
      this.loading = true
      const anyNo = this.anyNo
      // 退回单号、原物流商单号、原物流单号、混合条件查询
      this.queryParams.backOrderNo = undefined
      if (this.noType === 0) {
        this.queryParams.returnNo = anyNo
      }
      // 时间查询
      const timeRange = this.timeRange || 0
      this.queryParams.createdStart = undefined
      this.queryParams.createdEnd = undefined
      this.queryParams.timeType = undefined
      if (this.timeType === 0) {
        this.queryParams.timeType = 'CREATE'
        this.queryParams.gmtCreatedStart = timeRange[0] / 1000
        this.queryParams.gmtCreatedEnd = timeRange[1] / 1000
      }
      if (this.timeType === 1) {
        this.queryParams.timeType = 'HANDLE'
        this.queryParams.gmtCreatedStart = timeRange[0] / 1000
        this.queryParams.gmtCreatedEnd = timeRange[1] / 1000
      }
      const res = await getPageList({ ...this.queryParams })
      if (res.data) {
        this.tableData = res.data.noCustomers
        this.total = res.data.totalRowNum
      } else {
        this.tableData = []
        this.total = 0
      }
      this.loading = false
    },
    // 新增
    async handleAdd() {
      this.infoParams.operation = 1
      this.title = this.$t('add')
      this.depotDialog = true
    },
    // 查看
    async handleDetail(row) {
      this.infoParams.operation = 0
      this.infoParams.id = row.id
      this.title = this.$t('info')
      setTimeout(() => {
        this.depotDialog = true
      }, 400)
    },
    // 库存导出
    async handleExport() {
      const res = await exportStockList({})
      console.log(res, '库存导出')
    },
    // 关闭弹出层
    close() {
      this.infoParams.id = null
      this.infoParams.operation = 0
      this.depotDialog = false
      this.getDepotPageList()
    },
    // 切换页码
    pageNoChange(newPage) {
      this.queryParams.pageNo = newPage
      this.getDepotPageList()
    },
    indexAdd(index) {
      // console.log(index)
      const page = this.queryParams.pageNo // 当前页码
      const pagesize = this.queryParams.pageSize // 每页条数
      return index + 1 + (page - 1) * pagesize
    }
  }
}
</script>

<style scss="lang" scoped></style>
